﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SaleShop.DAL;
using SaleShop.Utils;

namespace SaleShop.BLL
{
    public class Product
    {
        /// <summary>
        /// Lấy danh sách sản phẩm
        /// </summary>
        /// <param name="status">Trạng thái (1:Hoạt động, 2:Khóa)</param>
        /// <param name="categoryID">Chuyên mục (0:Tất cả chuyên mục, !=0:Lấy theo chuyên mục)</param>
        /// <param name="accountName">Tài khoản post sản phẩm ("":Lấy tất cả, !="":Lấy theo tài khoản post sản phẩm)</param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public List<ProductInfo> SelectProduct(short status, int categoryID, string accountName, int page, int pageSize, ref int? totalRecord)
        {
            try
            {
                ProductDataContext context = new ProductDataContext(Setting.ConnectionString);
                return context.SelectProduct(status, categoryID, accountName, page, pageSize, ref totalRecord).ToList();
            }
            catch
            {                
                throw;
            }
        }

        /// <summary>
        /// Tìm kiếm sản phẩm
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public List<ProductInfo> SearchProduct(string keyword, int page, int pageSize, ref int? totalRecord)
        {
            try
            {
                ProductDataContext context = new ProductDataContext(Setting.ConnectionString);
                return context.SearchProduct(keyword, page, pageSize, ref totalRecord).ToList();
            }
            catch
            {                
                throw;
            }
        }

        /// <summary>
        /// Lấy danh sách sản phẩm đồng giá
        /// </summary>
        /// <param name="productID">Mã sản phẩm</param>
        /// <param name="priceOutput">Giá bán</param>
        /// <param name="priceSpace">Khoảng giá chênh lệch cho phép</param>
        /// <param name="categoryID">Mã chuyên mục (0:Tất cả chuyên mục))</param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public List<ProductInfo> SelectProductSamePrice(int productID, int priceOutput, int priceSpace, int categoryID, int page, int pageSize, ref int? totalRecord)
        {
            try
            {
                ProductDataContext context = new ProductDataContext(Setting.ConnectionString);
                return context.SelectProductSamePrice(productID, priceOutput, priceSpace, categoryID, page, pageSize, ref totalRecord).ToList();
            }
            catch
            {
                throw;                 
            }
        }

        /// <summary>
        /// Thêm mới sản phẩm
        /// </summary>
        /// <param name="cateogryID"></param>
        /// <param name="productName"></param>
        /// <param name="summary">Tóm tắt sản phẩm</param>
        /// <param name="priceInput">Giá nhập</param>
        /// <param name="priceOutput">Giá bán</param>
        /// <param name="saleOff">% giảm giá</param>
        /// <param name="quantity">Số lượng</param>
        /// <param name="image"></param>
        /// <param name="description">Chi tiết sản phẩm</param>
        /// <param name="status">1:Hoạt động;2:Khóa</param>        
        /// <param name="accountName"></param>
        /// <returns></returns>
        public int InsertProduct(int cateogryID, string productName, string summary, int priceInput, int priceOutput,
            short saleOff, short quantity, string image, string description, short status, string accountName)
        {
            try
            {
                ProductDataContext context = new ProductDataContext(Setting.ConnectionString);
                return context.InsertProduct(cateogryID, productName, summary, priceInput, priceOutput, saleOff, quantity, image,
                    description, status, accountName);
            }
            catch
            {                
                throw;
            }
        }

        /// <summary>
        /// Lấy thông tin 1 sản phẩm
        /// </summary>
        /// <param name="productID"></param>
        /// <returns></returns>
        public ProductInfo SelectProductByProductID(int productID)
        {
            try
            {
                ProductDataContext context = new ProductDataContext(Setting.ConnectionString);
                return context.SelectProductByProductID(productID).FirstOrDefault();
            }
            catch
            {                
                throw;
            }
        }

        /// <summary>
        /// Cập nhật thông tin sản phẩm
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="categoryID"></param>
        /// <param name="productName"></param>
        /// <param name="summary">Tóm tắt sản phẩm</param>
        /// <param name="priceInput">Giá nhập</param>
        /// <param name="priceOutput">Giá bán</param>
        /// <param name="saleOff">% giảm giá</param>
        /// <param name="quantity"></param>
        /// <param name="image"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public int UpdateProduct(int productID, int categoryID, string productName, string summary, int priceInput, int priceOutput, short saleOff, short quantity, string image, string description)
        {
            try
            {
                ProductDataContext context = new ProductDataContext(Setting.ConnectionString);
                return context.UpdateProduct(productID, categoryID, productName, summary, priceInput, priceOutput, saleOff, quantity, image, description);
            }
            catch
            {                
                throw;
            }
        }
    }
}
